const mongoose = require("mongoose");
const Schema = mongoose.Schema;

require("./User");

const CommentSchema = new Schema({
  user: {
    id: {
      type: Schema.Types.ObjectId,
      ref: "users",
    },
    name: {
      type: String,
      required: true,
    },
    avatar: {
      type: String,
      required: true,
    },
  },
  contentText: {
    type: String,
    required: true,
  },
  date: {
    type: Date,
    default: Date.now,
  },
  replyList: [
    {
      user: {
        id: {
          type: Schema.Types.ObjectId,
          ref: "users",
        },
        name: {
          type: String,
          required: true,
        },
        avatar: {
          type: String,
          required: true,
        },
      },
      contentText: {
        type: String,
        required: true,
      },
      date: {
        type: Date,
        default: Date.now,
      },
    },
  ],
});

const InfoStreamSchema = new Schema(
  {
    user: {
      id: {
        type: Schema.Types.ObjectId,
        ref: "users",
      },
      name: {
        type: String,
        required: true,
      },
      avatar: {
        type: String,
        required: true,
      },
    },
    title: {
      type: String,
      required: true,
    },
    imageUrl: {
      type: String,
      required: true,
    },
    content: {
      type: String,
      required: true,
    },
    comments: [CommentSchema],
    date: {
      type: Date,
      default: Date.now,
    },
  },
  { versionKey: false }
);

module.exports = InfoStream = mongoose.model("InfoStream", InfoStreamSchema);
